package com.hnevc.caoliuye;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 使用JDBC访问数据库
 * root root 3307 用户名 密码 端口号
 * localhost 访问地址
 * user 数据库名
 * student 数据库表名
 */
public class Example {
    public static void main(String[] args) {
        //1,注册数据库驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2,通过DriverManager获取数据库连接
            String url = "jdbc:mysql://localhost:3307/user";
            String username = "root";
            String password ="root";
            Connection connection = DriverManager.getConnection(url,username,password);
            //获取statement对象
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from student");
            while(resultSet.next()){
                System.out.println(resultSet.getString("name") + resultSet.getString("department"));
            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
